<?php
/**
 * Name: 用户实现.
 * User: Small_K
 * Date: 2018/1/12
 * Time: 17:27
 */

namespace App\Services\Callback;

use App\Models\User;
use App\Service\Interfaces\CallbackServiceInterface;
use Illuminate\Database\Eloquent\Model;
use Auth;

class AdminCallback implements CallbackServiceInterface
{
    /**
     *
     * 查询数据集合
     *
     * @param array|null $_filter 过滤条件
     * @param array|null $_order 排序条件
     * @param int $_limit 分页限制
     *
     * @return mixed
     */
    public function index(array $_filter = null, array $_order = null, $_limit = 20)
    {
        $result = User::where([['id', '>', 0],['status' ,'>=','0']]);
        //查询条件
        if (!empty($_filter)) {
            foreach ($_filter as $key => $value) {
                if (!empty($value)) {
                    foreach ($value as $query) {
                        $result->$key($query);
                    }
                }
            }
        }
        //排序
        if (!empty($_order)) {
            foreach ($_order as $key => $value) {
                $result->orderBy($key, $value);
            }
        }
        return $result->paginate($_limit);
    }

    /**
     *
     * 新增数据
     *
     * @param array|null $_options 数据参数
     *
     * @return Model
     */
    public function store(array $_options = null): Model
    {
        $user = new User();
        $user->name = isset($_options['name']) ? $_options['name'] : '';  //名称
        $user->mobile = isset($_options['mobile']) ? $_options['mobile'] : '';  //手机号
        $user->email = isset($_options['email']) ? $_options['email'] : '';  //邮箱
        $user->avatar = isset($_options['avatar']) ? $_options['avatar'] : '';  //头像
        $user->status = isset($_options['status']) ? $_options['status'] : '';  //状态['1'开启，0关闭]
        $user->password = isset($_options['password']) ? bcrypt($_options['name']) : '';  //密码
        $user->save();
        return $user;
    }

    /**
     *
     * 查询数据详情
     *
     * @param array $_filter 过滤条件
     *
     * @return Model
     */
    public function show(array $_filter): Model
    {
        $result = User::find($_filter)->first();
        return $result;
    }

    /**
     *
     * 修改数据
     *
     * @param array $_filter 过滤条件
     * @param array $_update_data 要修改的数据
     *
     * @return bool
     */
    public function update(array $_filter, array $_update_data): bool
    {
        $admin = User::find($_filter)->first();
        if (!is_null($admin)){
            $admin->name = $_update_data['name'];
            $admin->avatar = $_update_data['avatar'];
            $admin->mobile = $_update_data['mobile'];
            $admin->email = $_update_data['email'];
            $admin->status = $_update_data['status'];
            $admin->password = $_update_data['password'] ?? $admin->password;
            return $admin->save();
        }else{
            return false;
        }

    }

    /**
     *
     * 删除数据
     *
     * @param array $_filter 过滤条件
     *
     * @return bool
     */
    public function destroy(array $_filter): bool
    {
        $admin = User::find($_filter)->first();
        if (!is_null($admin)) {
            $admin->status = '-1';
            return $admin->save();
        } else {
            return false;
        }
    }
}